package com.zu.commu.landi2;

import com.zu.commu.context.ServerContext;
import com.zu.commu.interf.Action;
import com.zu.commu.interf.Command;
import com.zu.commu.interf.Data;
import com.zu.commu.interf.Order;
import com.zu.commu.interf.gprs.Handle;
import com.zu.commu.landi2.protocol.AnswerGPRS;
import com.zu.commu.landi2.protocol.HeartBeatProtocol;
import com.zu.commu.landi2.protocol.OnLineProtocol;
import org.apache.log4j.Logger;

public class HandleImp
  implements Handle
{
  private static Logger log = Logger.getLogger("com.automic.commu.landi2.RequestImp");
  private byte[] remoteData;
  private byte[] centerData;
  private Order order;
  private String error;

  public Action analyseData(byte[] data, String gprsId)
  {
    OnLineProtocol onp = new OnLineProtocol();
    try {
    	//是否是上线数据
      if (onp.isOnLineData(new String(data).trim())) {
        this.remoteData = onp.remoteData();
        return Action.SENDREMOTE;
      }
      String beatStr = (String)ServerContext.GPRSProtocol_heartBeatString.get("landi2");
      HeartBeatProtocol hbp = new HeartBeatProtocol();
     //是否是心跳
      if (hbp.isHeartBeat(new String(data).trim(), beatStr)) {
        String needReturn = (String)ServerContext.GPRSProtocol_returnHeartBeat.get("landi2");
        if (needReturn.equals("1"))
        {
          this.remoteData = beatStr.getBytes();
          return Action.SENDREMOTE;
        }
        return Action.NOACTION;
      }
    
      this.centerData = data;
      AnswerGPRS an = new AnswerGPRS();
      try {
        if (an.isNeedAnswer(data)) {
          this.remoteData = an.remoteData();
          return Action.SENDREMOTEANDCENTER;
        }
      } catch (Exception localException1) {
      }
      return Action.SENDCENTER;
    }
    catch (Exception e)
    {
      log.error(e.getMessage());
      this.error = e.getMessage();
    }return Action.ERROR;
  }

  public Action analyseOrder(Order order, Command command, byte[] data, String gprsId)
  {
    this.remoteData = data;
    return Action.SENDREMOTE;
  }

  public byte[] getRemoteData()
  {
    return this.remoteData;
  }

  public byte[] getCenterData()
  {
    return this.centerData;
  }

  public Order dataFromwhatOrder()
  {
    return this.order;
  }

  public Data[] getDirectData()
  {
    return null;
  }

  public String getError()
  {
    return this.error;
  }
}